package com.zhuozhengsoft.pageoffice6springboot2back.controller.insertSeal;

import com.zhuozhengsoft.pageoffice6springboot2back.util.GetDirPathUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.*;

@RestController
@RequestMapping(value = "/InsertSeal")
public class InsertSealController {
    //获取doc目录的磁盘路径
    private final String dir = GetDirPathUtil.getDirPath() + "static/doc/";

    public InsertSealController() throws IOException {
    }

    @RequestMapping(value = "/refresh")
    public String showrefresh(HttpServletRequest request) {

        copyFile(dir + "InsertSeal/Word/AddSeal1/word1_bak.docx", dir + "InsertSeal/Word/AddSeal1/word1.docx");
        copyFile(dir + "InsertSeal/Word/AddSeal2/word2_bak.docx", dir + "InsertSeal/Word/AddSeal2/word2.docx");
        copyFile(dir + "InsertSeal/Word/AddSeal3/word3_bak.docx", dir + "InsertSeal/Word/AddSeal3/word3.docx");
        copyFile(dir + "InsertSeal/Word/AddSeal4/word4_bak.docx", dir + "InsertSeal/Word/AddSeal4/word4.docx");
        copyFile(dir + "InsertSeal/Word/AddSeal5/test_bak.docx", dir + "InsertSeal/Word/AddSeal5/test.docx");
        copyFile(dir + "InsertSeal/Word/AddSeal6/test_bak.docx", dir + "InsertSeal/Word/AddSeal6/test.docx");
        copyFile(dir + "InsertSeal/Word/AddSeal7/test_bak.docx", dir + "InsertSeal/Word/AddSeal7/test.docx");
        copyFile(dir + "InsertSeal/Word/AddSeal8/word8_bak.docx", dir + "InsertSeal/Word/AddSeal8/word8.docx");
        copyFile(dir + "InsertSeal/Word/AddSeal9/word9_bak.docx", dir + "InsertSeal/Word/AddSeal9/word9.docx");
        copyFile(dir + "InsertSeal/Word/DeleteSeal/test_bak.docx", dir + "InsertSeal/Word/DeleteSeal/test.docx");


        copyFile(dir + "InsertSeal/Word/AddSign1/word1_bak.docx", dir + "InsertSeal/Word/AddSign1/word1.docx");
        copyFile(dir + "InsertSeal/Word/AddSign2/test_bak.docx", dir + "InsertSeal/Word/AddSign2/test.docx");
        copyFile(dir + "InsertSeal/Word/AddSign3/test_bak.docx", dir + "InsertSeal/Word/AddSign3/test.docx");
        copyFile(dir + "InsertSeal/Word/AddSign4/test_bak.docx", dir + "InsertSeal/Word/AddSign4/test.docx");
        copyFile(dir + "InsertSeal/Word/AddSign5/word2_bak.docx", dir + "InsertSeal/Word/AddSign5/word2.docx");

//        copyFile(dir + "InsertSeal/Word/BatchAddSeal/test1_bak.docx", dir + "InsertSeal/Word/BatchAddSeal/test1.docx");
//        copyFile(dir + "InsertSeal/Word/BatchAddSeal/test2_bak.docx", dir + "InsertSeal/Word/BatchAddSeal/test2.docx");
//        copyFile(dir + "InsertSeal/Word/BatchAddSeal/test3_bak.docx", dir + "InsertSeal/Word/BatchAddSeal/test3.docx");
//        copyFile(dir + "InsertSeal/Word/BatchAddSeal/test4_bak.docx", dir + "InsertSeal/Word/BatchAddSeal/test4.docx");


        copyFile(dir +  "InsertSeal/PDF/AddSeal1/test_bak.pdf", dir + "InsertSeal/PDF/AddSeal1/test.pdf");
        copyFile(dir +  "InsertSeal/PDF/AddSeal2/test_bak.pdf", dir + "InsertSeal/PDF/AddSeal2/test.pdf");
        copyFile(dir +  "InsertSeal/PDF/AddSeal3/hetong_bak.pdf", dir + "InsertSeal/PDF/AddSeal3/hetong.pdf");
        copyFile(dir +  "InsertSeal/PDF/DeleteSeal/test_bak.pdf", dir + "InsertSeal/PDF/DeleteSeal/test.pdf");
        copyFile(dir + "InsertSeal/PDF/AddSign1/test_bak.pdf", dir + "InsertSeal/PDF/AddSign1/test.pdf");

//        copyFile(dir + "InsertSeal/Excel/AddSeal/test1_bak.xlsx", dir + "InsertSeal/Excel/AddSeal/test1.xlsx");
//        copyFile(dir + "InsertSeal/Excel/AddSeal/test2_bak.xlsx", dir + "InsertSeal/Excel/AddSeal/test2.xlsx");
//        copyFile(dir + "InsertSeal/Excel/AddSeal/test3_bak.xlsx", dir + "InsertSeal/Excel/AddSeal/test3.xlsx");
//        copyFile(dir + "InsertSeal/Excel/AddSeal/test4_bak.xlsx", dir + "InsertSeal/Excel/AddSeal/test4.xlsx");
//        copyFile(dir + "InsertSeal/Excel/AddSeal/test5_bak.xlsx", dir + "InsertSeal/Excel/AddSeal/test5.xlsx");
//
//        copyFile(dir + "InsertSeal/Excel/AddSign/test1_bak.xlsx", dir + "InsertSeal/Excel/AddSign/test1.xlsx");
//        copyFile(dir + "InsertSeal/Excel/AddSign/test2_bak.xlsx", dir + "InsertSeal/Excel/AddSign/test2.xlsx");
//        copyFile(dir + "InsertSeal/Excel/AddSign/test3_bak.xlsx", dir + "InsertSeal/Excel/AddSign/test3.xlsx");

        return "复位成功！";
    }

    //拷贝文件
    private void copyFile(String oldPath, String newPath) {
        try {
            int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(oldPath);
            if (oldfile.exists()) { //文件存在时
                InputStream inStream = new FileInputStream(oldPath); //读入原文件
                FileOutputStream fs = new FileOutputStream(newPath);
                byte[] buffer = new byte[1444];
                int length;
                while ((byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread; //字节数 文件大小
//                    System.out.println(bytesum);
                    fs.write(buffer, 0, byteread);
                }
                fs.close();
                inStream.close();
            }
        } catch (Exception e) {
            System.out.println("复制单个文件操作出错");
            e.printStackTrace();
        }
    }


}
